﻿using System;
using System.IO;
using System.Windows.Forms;
using EventMgrLib;
using SmartMotion.Core;
using SmartMotion.Core.Mgrs;
using SmartMotion.Pages.DockPages;
using Sunny.UI;
using WeifenLuo.WinFormsUI.Docking;

namespace SmartMotion.Pages
{
    public partial class FrmFlowEdit : UIPage
    {
        private EventMgr _em;
        private string _configFile;
        private DeserializeDockContent _deserializeDockContent;
        private UnityIoc _container;
        private ActionItem _action;
        private Flow _flow;
        public FrmFlowEdit()
        {   
            InitializeComponent();
        }

        private void FrmFlowEdit_Load(object sender, EventArgs e)
        {
              _em=EventMgr.Ins;
              _container = UnityIoc.Ins;
              _em.GetEvent<ExitApp>().Subscribe(SaveConfig);
              _em.GetEvent<ShowWindow>().Subscribe(ShowIDEWindow);
              _em.GetEvent<ShowEditFrm>().Subscribe(ShowEditFrm);
              _em.GetEvent<GlobalControlEvent>().Subscribe(RunToolOrFLow,
                  ctrl=>ctrl==Controller.RunTool || ctrl==Controller.RunFlow);
              _em.GetEvent<SelectActionChanged>().Subscribe(ActionChanged);
              //加载上一次的配置

              LoadConfigFrmXml();
        }

        private void ActionChanged(ActionItem action)
        {
            _action = action;
        }
            
        private void RunToolOrFLow(Controller controller)
        {
            switch (controller)
            {
                case Controller.RunTool:
                    _action.Run();
                    break;
                case Controller.RunFlow:
                    _flow.Run();
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(controller), controller, null);
            }
        }

        private void ShowEditFrm(Flow flow)
        {

            Form selectForm = null;
            foreach (var dockPanel1Document in dockPanel1.Documents)
            {
                var frm = dockPanel1Document.DockHandler.Form;
                if (frm.Text!=flow.Name) continue;
                selectForm = frm;
                if (dockPanel1.ActiveDocument.DockHandler.Form.Text==flow.Name) return; //已打开
                break;
            }
            selectForm?.Close();

            _flow = flow;
            var frmEdit = _container.Resolve<FrmEdit>();
            frmEdit.Text = flow.Name;
            frmEdit.LoadFlow(flow);
            frmEdit.Show(dockPanel1);
        }

        private void ShowIDEWindow(WindowName windowName)
        {
            switch (windowName)
            {
                case WindowName.ToolBox:
                    _container.Resolve<FrmToolBox>().Show(dockPanel1);
                    break;
                case WindowName.Debug:
                    _container.Resolve<FrmDebug>().Show(dockPanel1);
                    break;
                case WindowName.Parms:
                    _container.Resolve<FrmParms>().Show(dockPanel1);
                    break;
                case WindowName.Solution:
                    _container.Resolve<FrmSolutionMgr>().Show(dockPanel1);
                    break;
                case WindowName.Property:
                    _container.Resolve<FrmProperty>().Show(dockPanel1);
                    break;
                case WindowName.Var:
                    _container.Resolve<FrmVar>().Show(dockPanel1);
                    break;
                case WindowName.Default:
                    DefaultLayout();
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(windowName), windowName, null);
            }   
        }

        private void LoadConfigFrmXml()
        {
            dockPanel1.Theme = new VS2015BlueTheme();
            try
            {
                _deserializeDockContent = GetContentFromPersistString;
                _configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
                if (File.Exists(_configFile))
                {
                    dockPanel1.LoadFromXml(_configFile, _deserializeDockContent);
                }
            }
            catch (Exception e)
            {
                DefaultLayout();
            }
        }

        private IDockContent GetContentFromPersistString(string persistString)
        {
            if (persistString==typeof(FrmToolBox).ToString())
            {
                return _container.Resolve<FrmToolBox>();
            }
            if (persistString == typeof(FrmDebug).ToString())
            {
                return _container.Resolve < FrmDebug>();
            }
            if (persistString == typeof(FrmParms).ToString())
            {
                return _container.Resolve < FrmParms>();
            }
            if (persistString == typeof(FrmSolutionMgr).ToString())
            {
                return _container.Resolve < FrmSolutionMgr>();
            }
            if (persistString == typeof(FrmProperty).ToString())
            {
                return _container.Resolve < FrmProperty>();
            }
            if (persistString == typeof(FrmVar).ToString())
            {
                return _container.Resolve < FrmVar>();
            }
            return null;
        }
        private void SaveConfig()
        {
            _configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
            dockPanel1.SaveAsXml(_configFile);
        }

        private void DefaultLayout()    
        {
            //添加工具箱
            _container.Resolve<FrmToolBox>().Show(dockPanel1, DockState.DockLeft);
            dockPanel1.DockLeftPortion = 240;
            //添加调试面板
            _container.Resolve<FrmDebug>().Show(dockPanel1, DockState.DockBottom);
            dockPanel1.DockBottomPortion = 240;
            //参数
            _container.Resolve<FrmParms>().Show(dockPanel1, DockState.DockBottom);
            //流程管理器
            _container.Resolve<FrmSolutionMgr>().Show(dockPanel1, DockState.DockRight);
            dockPanel1.DockRightPortion = 240;
            _container.Resolve<FrmProperty>().Show(dockPanel1, DockState.DockRight);
            //全局变量
            _container.Resolve<FrmVar>().Show(dockPanel1);
        }

        private void dockPanel1_ActiveContentChanged(object sender, EventArgs e)
        {
            string frmName = dockPanel1.ActiveDocument.DockHandler.Form.Text;
            var flow = FlowsMgr.Ins.GetFlow(frmName);
            if (flow!=null)
            {
                if (_flow!=flow)
                {
                    _em.GetEvent<SelectedObjectChanged>().Publish(flow);
                }

                _flow = flow;
            }
        }

        private void FrmFlowEdit_FormClosing(object sender, FormClosingEventArgs e)
        {
            _em.GetEvent<ExitApp>().Unsubscribe(SaveConfig);
            _em.GetEvent<ShowWindow>().Unsubscribe(ShowIDEWindow);
            _em.GetEvent<ShowEditFrm>().Unsubscribe(ShowEditFrm);
            _em.GetEvent<GlobalControlEvent>().Unsubscribe(RunToolOrFLow);
            _em.GetEvent<SelectActionChanged>().Unsubscribe(ActionChanged);
        }
    }   
}
    